package cn.stard._04_rocketmq_tran;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 1.创建消费对象,指定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("delay_hello_group");
        // 2.连接mq,消息队列中间件解耦合
            consumer.setNamesrvAddr("127.0.0.1:9876");
        // 4.消费者订阅的主题,表示消费者消费那个主题消息,二级分类可以是*
        consumer.subscribe("delay_topic","hello_tags");
        // 5.设置监听器
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg : msgs) {
                    String result = new String(msg.getBody(), StandardCharsets.UTF_8);
                    System.out.println(result);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        // 3.启动消费者
        consumer.start();
    }
}





